package com.ego.service.impl;


import com.ego.commons.pojo.EasyUIDatagrid;
import com.ego.commons.pojo.EgoResult;
import com.ego.dubbo.service.TBContentDubboService;
import com.ego.pojo.TbContent;
import com.ego.rabbitmq.send.PublishMessage;
import com.ego.service.TBContentService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TBContentServiceImpl implements TBContentService {

    @DubboReference
    private TBContentDubboService tbContentDubboService;


    @Value("${ego.bigad.categoryId}")
    private long categoryId;


    @Value("${ego.updateBigAdCache.exchange}")
    private String exchange;

    @Value("${ego.updateBigAdCache.routingKey}")
    private String routingKey;


    @Autowired
    private PublishMessage publishMessage;


    @Override
    public EgoResult insert(TbContent tbContent) {

        Date now = new Date();

        tbContent.setCreated(now);
        tbContent.setUpdated(now);

        int row = tbContentDubboService.insert(tbContent);

        //判断当前是否是新增大广告
        if(tbContent.getCategoryId()==categoryId){

            publishMessage.asyncSenderMessage(exchange,routingKey,"新增广告了...");
        }


        if(row == 1){
            return EgoResult.ok();
        }

        return EgoResult.error("新增内容失败");
    }



    @Override
    public EgoResult update(TbContent tbContent) {

        tbContent.setUpdated(new Date());

        int rows = tbContentDubboService.update(tbContent);
        //判断当前是否是新增大广告
        if(tbContent.getCategoryId()==categoryId){
            publishMessage.asyncSenderMessage(exchange,routingKey,"修改广告了...");
        }

        if(rows == 1){
            return EgoResult.ok();
        }

        return EgoResult.error("修改内容失败");
    }

    @Override
    public EgoResult deleteByIds(List<Long> ids) {

        boolean flag = false;
        //根据删除id查询出对应的内容信息，并且要获取categoryid，判断这些category是否是大广告分类，只要有一个是，就要发送消息队列，更新缓存
        //判断批量删除的categoryid是否包含大广告分类id
        List<TbContent> tbContents = tbContentDubboService.selectByIds(ids);
        for (TbContent tbContent : tbContents) {
            if(tbContent.getCategoryId()==categoryId){
                //查询出所有大广告内容
                flag = true;
                break;
            }
        }

        int result = tbContentDubboService.deleteByIds(ids);

        if (result == 1){

            if(flag){
                //数据库删除成功以后发送消息，让消息队列消费者去判断删除id的分类id是否大广告，将耗时任务交给mq处理
                publishMessage.asyncSenderMessage(exchange,routingKey,"删除大广告数据了...");
            }

            return EgoResult.ok();
        }

        return EgoResult.error("删除内容失败，请联系管理员!");
    }


    @Override
    public EasyUIDatagrid selectTbContentPageByCategoryId(long categoryId, int page, int rows) {


        PageInfo<TbContent> pageInfo = tbContentDubboService.selectTbContentPageByCategoryId(categoryId, page, rows);

        return new EasyUIDatagrid(pageInfo.getList(),pageInfo.getTotal());
    }
}
